﻿using System;
abstract class Figure
{
    public abstract double area();
    public abstract bool judge();    
}
class Triangular : Figure
{
    int a { get; set; }
    int b { get; set; }
    int c { get; set; }
    public Triangular(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public override double area()
    {
        if (judge())
        {
            double p = (a + b + c) / 2;
            return System.Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        }
        else return 0;
    }
    public override bool judge()
    {
        if (a + b > c && a + c > b && b + c > a)
        {
            return true;
        }
        else return false;
    }
}
class Rectangular : Figure
{
    int a { get; set; }
    int b { get; set; }
    public Rectangular(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
    public override double area()
    {
        if (judge())
        {
            return a * b;
        }
        else return 0;
    }
    public override bool judge()
    {
        if (a > 0 && b> 0)
        {
            return true;
        }
        else return false;
    }
}
class Square : Figure
{
    int a { get; set; }
    public Square(int a)
    {
        this.a = a;
    }
    public override double area()
    {
        if (judge())
        {
            return a * a;
        }
        else return 0;
    }
    public override bool judge()
    {
        if (a > 0)
        {
            return true;
        }
        else return false;
    }
}
class Factory
{
    public static Figure GetProduct(string arg)
    {
        string[] s = arg.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 3)
        {
            int a = int.Parse(s[0]);
            int b = int.Parse(s[1]);
            int c = int.Parse(s[2]);
            Console.WriteLine("创建了一个边长为{0}, {1}, {2}的三角形", a, b, c);
            return new Triangular(a, b, c);
        }
        else if (s.Length == 2)
        {
            int a = int.Parse(s[0]);
            int b = int.Parse(s[1]);
            Console.WriteLine("创建了一个边长为{0}, {1}的长方形", a, b);
            return new Rectangular(a, b);
        }
        else if (s.Length == 1)
        {
            int a = int.Parse(s[0]);
            Console.WriteLine("创建了一个边长为{0}的正方形", a);
            return new Square(a);
        }
        else
        {
            return null;
        }
    }
}
class Program
{
    public static void Main()
    {
        Random rnd = new Random();
        int j = 0, a = 0, b = 0, c = 0;
        Figure[] product = new Figure[10];
        string str = null;
        for (int i = 0; i < 10; i++)
        {
            j = rnd.Next() % 3 + 1;
            Console.WriteLine("第{0}次：", i);
            switch (j)
            { 
                case 1:
                    a = rnd.Next() % 100;
                    str = a.ToString();
                    product[i] = Factory.GetProduct(str);
                    break;
                case 2:
                    a = rnd.Next() % 100;
                    b = rnd.Next() % 100;
                    while (b == a)
                        b = rnd.Next() % 100;
                    str = a.ToString() + ", " + b.ToString();
                    product[i] = Factory.GetProduct(str);
                    break;
                case 3:
                    a = rnd.Next() % 100;
                    b = rnd.Next() % 100;
                    c = rnd.Next() % 100;
                    str = a.ToString() + ", " + b.ToString() + ", " + c.ToString();
                    product[i] = Factory.GetProduct(str);
                    break;
                default:
                    Console.WriteLine("第{0}次创建失败，重新创建", i);
                    i -= 1;
                    continue;
                    break;
            }
            if (!product[i].judge())
            {
                Console.WriteLine("第{0}次创建图形不合法，重新创建", i);
                i -= 1; 
            }
            else
            {
                Console.WriteLine();
            }
        }
    }
}
